<?php

namespace App\Admin\Controllers;

use App\Models\Type;
use App\Models\Video;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use http\Env\Request;

class VideoController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '影视管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Video());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'))->width(50)->sortable();
        $grid->column('video_type', __('分类id'))->width(70)->display(function ($type){
            return $type['name'];
        });
        $grid->column('name', __('名称'))->width(100)->limit(10);
        $grid->column('img', __('图片'))->image(null,50,50);
        $grid->column('desc', __('描述'))->width(200)->limit(20);
        $grid->column('director', __('导演'))->width(100)->limit(10);
        $grid->column('performer', __('演员'))->width(100)->limit(10);
        $grid->column('url', __('地址'))->downloadable();
        $grid->column('num', __('观看人数'))->width(80);
        $grid->column('fraction', __('分数'))->width(50);
        $grid->column('created_at', __('创建时间'))->width(100);
        $grid->column('updated_at', __('修改时间'))->width(100);
        $grid->column('isdelete', __('状态'))->replace([-1=>"冻结",0=>"删除",1=>"正常"])->label([
            1 => 'success',
            -1 => 'danger',
        ]);
        $grid->quickSearch('name');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Video::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('video_type', __('分类id'))->as(function ($type){
            return $type['name'];
        });
        $show->field('name', __('名称'));
        $show->field('img', __('图片'))->image();
        $show->field('desc', __('描述'));
        $show->field('director', __('导演'));
        $show->field('performer', __('演员'));
        $show->field('url', __('地址'));
        $show->field('num', __('观看人数'));
        $show->field('fraction', __('分数'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('修改时间'));
        $show->field('isdelete', __('状态'))->using([0=>"冻结",1=>"正常"]);

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Video());
        $form->select('type_id', __('分类id'))->options($this->type(1));
        $form->text('name', __('名称'));
        $form->image('img', __('图片'));
        $form->textarea('desc', __('描述'));
        $form->text('director', __('导演'));
        $form->text('performer', __('演员'))->placeholder('多个演员用英文逗号,隔开');
        $form->url('url', __('地址'));
        $form->number('num', __('观看人数'));
        $form->number('fraction', __('分数'));
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => -1, 'text' => '冻结', 'color' => 'danger'],
        ];
        $form->radio('param',__('其他类型'))->options($this->type(3));
        $form->switch('isdelete', __('状态'))->default(1)->states($states);

        return $form;
    }

    /**
     * 获取影视分类
     * @return array
     */
    public function type($parent)
    {
        $data = Type::where('parent_id',$parent)->select('id','name')->get()->toArray();
        $_id = [];
        $_name = [];
        foreach ($data as $key=>$value){
            array_push($_id,$data[$key]['id']);
            array_push($_name,$data[$key]['name']);

        }
        return array_combine($_id,$_name);
    }
}
